package com.jtxy.npms.controller;

import com.jtxy.npms.entity.AQI;
import com.jtxy.npms.entity.AQIFeedback;
import com.jtxy.npms.utils.FileUtils;
import com.jtxy.npms.utils.Utils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SupervisorAddAQIViewController {
    @FXML
    private TableView tableView;
    //    表格列
    @FXML
    private TableColumn<AQI, String> levelColumn;
    @FXML
    private TableColumn<AQI, String> descriptionColumn;
    @FXML
    private TableColumn<AQI, String> impactionColumn;
    @FXML
    private TextField txtAddress;
    @FXML
    private TextArea txtFeedback;
    //    下拉列表所在省
    @FXML
    private ComboBox<String> provinceComboBox;
    //    所在市
    @FXML
    private ComboBox<String> cityComboBox;
    //    预估等级
    @FXML
    private ComboBox<String> levelComboBox;

    //一对多的省市关系，键为省，值为市（多个）
    private Map<String, List<String>> provinceMap = new HashMap<>();

    @FXML
    //        设置数据源，观察者模式来监听数据源的变化
    private ObservableList<AQI> aqiList = FXCollections.observableArrayList();

    //初始化表格，将数据读入到表格中去
    @FXML
    public void initialize() {
//        绑定数据源,只要向数据源list中添加数据，表格就会自动更新
        tableView.setItems(aqiList);
        //绑定类的属性到表格的列
        levelColumn.setCellValueFactory(new PropertyValueFactory<>("level"));
        descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
        impactionColumn.setCellValueFactory(new PropertyValueFactory<>("impaction"));
        Map<String, AQI> AqiMap = FileUtils.readObject("aqi.txt");
//        读取map中的每个数据，添加进aqiList
        aqiList.addAll(AqiMap.values());

//        初始化下拉列表数据
        provinceMap.put("内蒙古", List.of("赤峰", "包头", "呼伦贝尔"));
        provinceMap.put("辽宁", List.of("沈阳", "大连", "鞍山"));
        provinceMap.put("吉林", List.of("长春", "吉林", "四平"));
//        添加所有的省到省的下拉列表中
        provinceComboBox.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
//        监听省下拉列表的选中项
        provinceComboBox.getSelectionModel().selectedItemProperty().addListener((new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                List<String> cities = provinceMap.get(newValue);
                //        添加市到下拉列表中
                cityComboBox.setItems(FXCollections.observableArrayList(cities));
            }
        }));
        levelComboBox.setItems(FXCollections.observableArrayList("一级", "二级", "三级", "四级", "五级", "六级"));

    }
//    保存AQI反馈信息
    public void saveAQIFeedback() {
        //        先读取文件
        Map<String, AQIFeedback> aqiFeedbackMap = FileUtils.readObject("aqiFeedback.txt");
//        当前的记录数加1，是新数据的id
        String afId = String.valueOf(aqiFeedbackMap.size() + 1);
        String telId = LoginViewController.currentLoginSupervisor.getTelId();
        String province = provinceComboBox.getValue();
        String city = cityComboBox.getValue();
        String level = levelComboBox.getValue();
        String address = txtAddress.getText();
        String feedback = txtFeedback.getText();
        String date= LocalDate.now().toString();

        AQIFeedback aqiFeedback = new AQIFeedback(afId, telId, province, city, address, level, feedback, date);

        aqiFeedbackMap.put(afId, aqiFeedback);
        FileUtils.writeObject("aqiFeedback.txt", aqiFeedbackMap);
        Utils.showDialog("AQI反馈", "信息反馈成功");
    }

}
